package com.ghck.server.bkcontroller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.ghck.shop.model.Page;
import com.ghck.shop.model.ShippingChannels;
import com.ghck.shop.model.request.OrderInfoReq;
import com.ghck.shop.model.response.OrderInfoRes;
import com.ghck.shop.service.OrderService;


@Controller
@RequestMapping("/system/order")
@Api(description="管理员订单相关接口")
public class AdminOrderController {

	
	private final static Logger LOGGER = LoggerFactory.getLogger(AdminOrderController.class);
	
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ApiOperation(value="订单列表")
	public String list(OrderInfoReq orderInfoReq,HttpServletRequest request){
//		int start = 0;
//		if(currentPage > 0 ){
//			start = ((currentPage-1)*pageSize);
//		}
//		if(orderInfoReq.getOrderStatus() != null){
//			if((orderInfoReq.getOrderStatus()+100)==0){
//				orderInfoReq.setOrderStatus(null);
//			}
//		}
//		orderInfoReq.setStart(start);
//		orderInfoReq.setLimit(pageSize);
//		LOGGER.info("查询订单列表参数为:{}", JSONObject.toJSONString(orderInfoReq));
//		Page<OrderInfoRes> page = new Page<OrderInfoRes>(currentPage);
//		page.setPageSize(pageSize);
		List<OrderInfoRes> list = orderService.adminOrderList(orderInfoReq);
//		page.setResultRows(list);
//		page.setTotalRow(orderService.countAdminOrderList(orderInfoReq));
		request.setAttribute("list", list);
		request.setAttribute("orderInfoReq", orderInfoReq);
		request.setAttribute("shippingChannels", orderService.getAllShippingChannels());
		return "back/order/order_list";
	}
	@ApiOperation(value="订单详情")
	@RequestMapping(value="/detail/{orderId}",method=RequestMethod.GET)
	public String detail(HttpServletRequest request,@PathVariable("orderId")Integer orderId){
		OrderInfoReq orderInfoReq = new OrderInfoReq();
		orderInfoReq.setId(orderId);
		OrderInfoRes orderInfoRes = orderService.orderDetail(orderInfoReq);
		List<ShippingChannels> shippingChannelsList = orderService.getAllShippingChannels();
		request.setAttribute("order", orderInfoRes);
		request.setAttribute("shippingChannelsList", shippingChannelsList);
		//request.setAttribute("json", JSONObject.toJSONString(orderInfoRes));
		return "back/order/detail";
	}
	
	@ApiOperation(value="管理员发货")
	@RequestMapping(value="/update_shipping",method=RequestMethod.POST)
	public String updateOrderShippingChannel(HttpServletRequest request,OrderInfoReq orderInfoReq){
		orderService.updateOrderShippingChannel(orderInfoReq);
		return "back/order/order_list";
	}
}
